package com.yt.user.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.user.models.bo.StudentInfoBO;
import com.yt.user.models.bo.StudentInfoSaveBO;
import com.yt.user.models.dto.LoginInfoDTO;
import com.yt.user.models.dto.StudentInfoDTO;
import com.yt.user.models.service.ILoginInfoService;
import com.yt.user.models.service.IStudentInfoService;
import com.yt.user.models.vo.StudentInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @author
 * @dese
 * @date 2025/10/23 14:32
 */
@RestController
@RequestMapping("/api/studentInfo")
@Slf4j
@Api(value = "StudentInfoApiController", tags = "学生信息Api接口")
public class StudentInfoApiController {

    @DubboReference
    private IStudentInfoService studentInfoService;
    @DubboReference
    private ILoginInfoService loginInfoService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<StudentInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        StudentInfoDTO dto = studentInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "学生信息列表", notes = "学生信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<StudentInfoDTO>> queryPageList(StudentInfoBO bo) {
        Wrapper<StudentInfoDTO> wrapper = new EntityWrapper<>();
        if (bo.getStatus() != null) {
            wrapper.eq("status", bo.getStatus());
        }
        if (bo.getStartDate() != null) {
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null) {
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<StudentInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = studentInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册", notes = "用户注册")
    public Result<Boolean> register(@RequestBody StudentInfoSaveBO saveBO) {
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //判断手机号是否已经被注册
        Wrapper<LoginInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("account", saveBO.getPhone());
        LoginInfoDTO dto = loginInfoService.selectOne(wrapper);
        if (dto != null) {
            return new Result<>(CodeEnum.FAIL_AddUser_3004);
        }
        //增加学生信息和登录信息
        boolean flag = studentInfoService.addCus(saveBO);

        return new Result<>(flag);
    }

}

